﻿/*
 *  Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
 *  Copyright 2009, 2010 Matvei Stefarov <me@matvei.org>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 *
 */
using System;
using System.Text;
using ForgeCraft;
using System.IO;
using System.Diagnostics;

namespace ForgeCraft_Console {
    static class Program {
        static bool DidShutdown = false;

        static void Main(string[] args) {
            Server.OnLog += Log;
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(HandleShutdown);

            try {
                if (Server.Init()) {
                    Process.GetCurrentProcess().PriorityClass = Config.GetProcessPriority();

                    if (Server.Start()) {
                        Console.WriteLine("We're in the pipe five by five!");

                        string input = "";

                        while ((input = Console.ReadLine()) != "/exit")
                            Player.Console.ParseMessage(input, true);

                        Server.Shutdown();
                        DidShutdown = true;
                    } else {
                        Console.WriteLine("[!] Critical Error!");
                        Server.Shutdown();
                        DidShutdown = true;
                        Console.ReadLine();
                    }
                } else {
                    Console.WriteLine("[!] Failed to Initialize");
                    Server.Shutdown();
                    DidShutdown = true;
                    Console.ReadLine();
                }
            } catch (Exception ex) {
                System.IO.File.WriteAllText("crash.log", ex + Environment.NewLine + ex.StackTrace);
                Console.WriteLine("ForgeCraft crashed! Crash message saved to crash.log.");
                Console.Write(ex);
                Console.Title = "ForgeCraft crashed!";
            } finally {
                HandleShutdown(null, null);
            }
        }

        static void Log(string message, LogType type) {

            switch (type)
            {
                case LogType.Debug:
                case LogType.Testing:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case LogType.WorldChat:
                case LogType.ClassChat:
                case LogType.GlobalChat:
                    Console.ForegroundColor = ConsoleColor.Green;
                    break;
                case LogType.PrivateChat:
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    break;
                case LogType.Error:
                case LogType.SuspiciousActivity:
                case LogType.FatalError:
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
                case LogType.UserCommand:
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    break;
                case LogType.IRC:
                    Console.ForegroundColor = ConsoleColor.DarkMagenta;
                    break;
                case LogType.ConsoleInput:
                case LogType.ConsoleOutput:
                case LogType.DBActivity:
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    break;
                case LogType.SystemActivity:
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
                case LogType.UserActivity:
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                    break;
            }

            Console.WriteLine(message);
            Console.ForegroundColor = ConsoleColor.White;
            //Console.BackgroundColor = ConsoleColor.Black;
        }

        static void HandleShutdown(object sender, EventArgs e) {
            if (!DidShutdown)
                Server.Shutdown();
        }
    }
}